/*
* CL_fx.java
* Copyright (C) 2004
* 
* $Id: CL_inv.java,v 1.3 2005/12/18 22:10:10 cawe Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// Created on 31.01.2004 by RST.
using System;
using Defines = Suake2.UI.Defines;
using Globals = Suake2.UI.Globals;
using Com = Suake2.UI.qcommon.Com;
using MSG = Suake2.UI.qcommon.MSG;
using Lib = Suake2.UI.util.Lib;
using Vargs = Suake2.UI.util.Vargs;
namespace Suake2.UI.client
{
	
	/// <summary> CL_inv</summary>
	public class CL_inv
	{
		
		/*
		* ================ CL_ParseInventory ================
		*/
		internal static void  ParseInventory()
		{
			int i;
			
			for (i = 0; i < Defines.MAX_ITEMS; i++)
				Globals.cl.inventory[i] = MSG.ReadShort(Globals.net_message);
		}
		
		/*
		* ================ Inv_DrawString ================
		*/
		internal static void  Inv_DrawString(int x, int y, System.String string_Renamed)
		{
			for (int i = 0; i < string_Renamed.Length; i++)
			{
				Globals.re.DrawChar(x, y, string_Renamed[i]);
				x += 8;
			}
		}
		
		internal static System.String getHighBitString(System.String s)
		{
			sbyte[] b = Lib.stringToBytes(s);
			for (int i = 0; i < b.Length; i++)
			{
				b[i] = (sbyte) (b[i] | 128);
			}
			return Lib.bytesToString(b);
		}
		
		/*
		* ================ CL_DrawInventory ================
		*/
		internal const int DISPLAY_ITEMS = 17;
		
		internal static void  DrawInventory()
		{
			int i, j;
			int num, selected_num, item;
			int[] index = new int[Defines.MAX_ITEMS];
			System.String string_Renamed;
			int x, y;
			System.String binding;
			System.String bind;
			int selected;
			int top;
			
			selected = Globals.cl.frame.playerstate.stats[Defines.STAT_SELECTED_ITEM];
			
			num = 0;
			selected_num = 0;
			for (i = 0; i < Defines.MAX_ITEMS; i++)
			{
				if (i == selected)
					selected_num = num;
				if (Globals.cl.inventory[i] != 0)
				{
					index[num] = i;
					num++;
				}
			}
			
			// determine scroll point
			top = selected_num - DISPLAY_ITEMS / 2;
			if (num - top < DISPLAY_ITEMS)
				top = num - DISPLAY_ITEMS;
			if (top < 0)
				top = 0;
			
			x = (Globals.viddef.width - 256) / 2;
			y = (Globals.viddef.height - 240) / 2;
			
			// repaint everything next frame
			SCR.DirtyScreen();
			
			Globals.re.DrawPic(x, y + 8, "inventory");
			
			y += 24;
			x += 24;
			Inv_DrawString(x, y, "hotkey ### item");
			Inv_DrawString(x, y + 8, "------ --- ----");
			y += 16;
			for (i = top; i < num && i < top + DISPLAY_ITEMS; i++)
			{
				item = index[i];
				// search for a binding
				//Com_sprintf (binding, sizeof(binding), "use %s",
				// cl.configstrings[CS_ITEMS+item]);
				binding = "use " + Globals.cl.configstrings[Defines.CS_ITEMS + item];
				bind = "";
				for (j = 0; j < 256; j++)
					if (Globals.keybindings[j] != null && Globals.keybindings[j].Equals(binding))
					{
						bind = Key.KeynumToString(j);
						break;
					}
				
				string_Renamed = Com.sprintf("%6s %3i %s", new Vargs(3).add(bind).add(Globals.cl.inventory[item]).add(Globals.cl.configstrings[Defines.CS_ITEMS + item]));
				if (item != selected)
					string_Renamed = getHighBitString(string_Renamed);
				// draw a blinky cursor by the selected item
				else
				{
					if (((int) (Globals.cls.realtime * 10) & 1) != 0)
						Globals.re.DrawChar(x - 8, y, 15);
				}
				Inv_DrawString(x, y, string_Renamed);
				y += 8;
			}
		}
	}
}